<template>
  <div class="search-wrapper">
    <el-row class="search-bar" type="flex" justify="space-between">
      <input
        ref="inpi"
        type="text"
        placeholder="请输入想去的地方，比如：'广州'"
      />
      <i class="el-icon-search" @click="search"></i>
    </el-row>

    <div class="search-recommend">
      推荐:
      <a
        style=""
        v-for="(item, index) in city"
        :key="index"
        @click="sendSuggestion(item)"
      >
        <span>{{ item }}</span>
      </a>
    </div>
  </div>
</template>

<script>
export default {
  props: ["postList"],
  data() {
    return {
      keyword: [],
      city: ["广州", "北京", "青岛"]
    };
  },
  // created() {
  //   this.getCity();
  // },

  methods: {
    // getCity() {
    //   this.$axios({
    //     url: "/posts",
    //     params: {
    //       city: (this.cityName = "" ? "广州市" : this.cityName)
    //     }
    //   }).then(res => {
    //     // console.log(res);
    //     this.cityList = res.data;
    //     // console.log(this.cityList);
    //     this.$emit("postlistsearch", this.cityList);
    //   });
    // },

    search() {
      if (this.$refs.inpi.value.includes("广")) {
        this.$emit("postlistsearch", "广州市");
        return;
      }
      // 这里打印了你输入的文字
      console.log(this.$refs.inpi.value);
      // 如果有文字
      if (this.$refs.inpi.value) {
        // 打印所有的城市
        console.log(this.postList);
        // 过滤城市
        this.keyword = this.postList.filter(e => {
          // 如果哪个城市的文字包含你输入的文字
          if (e.city.name.includes(this.$refs.inpi.value)) {
            return true;
          } else {
            return false;
          }
        });
        console.log(this.keyword);
        // this.cityName = this.keyword[0].cityName;
        // console.log(this.cityName);
        // this.getCity();
        // console.log("123", this.cityList);
        // this.$emit("postlistsearch", this.getCity);
        this.$emit("postlistsearch", this.keyword[0].cityName);
        // console.log(this.keyword);
      } else {
        console.log(this.postList);
        this.$emit("postlistsearch", this.postList);
      }
    },
    sendSuggestion(item) {
      console.log(item);
      if (item === "广州") {
        this.$emit("postlistsearch", "广州市");
        return;
      }
      this.keyword = this.postList.filter(e => {
        if (e.city.name.includes(item)) {
          return true;
        } else {
          return false;
        }
      });
      // console.log(this.keyword);

      // this.cityName = this.keyword[0].cityName;
      // this.getCity();
      this.$emit("postlistsearch", this.keyword[0].cityName);
    }
  }
};
</script>

<style lang="less" scoped>
.search-bar {
  width: 100%;
  box-sizing: border-box;
  height: 40px;
  line-height: 40px;
  border: 3px solid orange;

  input {
    flex: 1;
    padding: 0 20px;
    line-height: 40px;
    outline: none;
    border: none;
    background: none;
  }

  i {
    position: absolute;
    top: 50%;
    right: 0;
    transform: translate(-50%, -50%);
    font-size: 24px;
    color: orange;
    font-weight: 700;
  }
}

.search-recommend {
  padding: 10px 0;
  font-size: 12px;
  color: #666;
  a:hover {
    color: orange;
  }

  span {
    margin: 0 10px;
  }
}
</style>
